package com.kinglone.back.module.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.kinglone.back.module.model.Module;
import com.kinglone.back.module.service.ModuleService;
import com.kinglone.back.permission.model.Permission;
import com.kinglone.back.permission.service.PermissionService;
import com.kinglone.common.utils.JSONMessageView;
import com.kinglone.common.utils.StringUtil;
/**
 * 权限管理控制层
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/back/module")
public class ModuleController {

	@Autowired
	private ModuleService moduleService;
	
	/**
	 * 跳转菜单管理页面
	 * @return
	 */
	@RequestMapping("/admin-module.htm")
	public ModelAndView admin_permission(){
		ModelAndView mv = new ModelAndView("back/module/admin-module");
		return mv;
	}
	
	/**
	 * 异步加载所有的模块
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/loadAllModule.do",produces={"application/json;charset=utf-8"})
	public String loadAllModule(){
		 String moduleInfo = "{}";
		try {
			moduleInfo = moduleService.findAllModule();					
		} catch (Exception e) {
			e.printStackTrace();
		}
		return moduleInfo;
	}
	
	/**
	 * 根据父级模块的code获取子模块信息   
	 * @return
	 */
	@RequestMapping("/getModulesByParent.htm")
	public ModelAndView getModulesByParent(Module module,Model model){
		ModelAndView mv = new ModelAndView("back/module/admin-modules-list");
		 //根据父级模块的code获取子模块信息
		 List<Module> modules = moduleService.getModulesByParent(module.getId());
		 model.addAttribute("modules", modules);
		 model.addAttribute("pid", module.getId());//将左边的父id传到子模块
		 System.out.println("----------传入左边id:"+module.getId());
		return mv;
	}
	/**
	 * 跳转添加模块页面
	 * @return
	 */
	@RequestMapping("/admin-add-module.htm")
	public ModelAndView showAddModule(@RequestParam(value="parentCode")String parentCode,Model model){
		ModelAndView mv = new ModelAndView("back/module/admin-add-module");
		model.addAttribute("parentCode", parentCode);
		System.out.println("-------------------------parentCode:"+parentCode);
		return mv;
		
	}
	/**
	 * 跳转到修改模块页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/updateModule.htm")
	public ModelAndView updateModule(@RequestParam(value="id")String id,@RequestParam(value="pid")String pid,Model model){
		ModelAndView mv = new ModelAndView("back/module/admin-add-module");
		Module module = moduleService.findModuleById(id);
		model.addAttribute("module", module);	
		model.addAttribute("parentCode", pid);
		return mv;
	}
	/**
	 * 添加或修改模块
	 * @param permission
	 * @return
	 */
	@RequestMapping("/moduleSave.htm")
	public JSONMessageView moduleSave(Module module){
		JSONMessageView msg = new JSONMessageView();
		try {
			if(StringUtil.isNotEmpty(module.getId())){
				moduleService.update(module);
				msg =new JSONMessageView(0,"修改成功!",null);	
			}else{
				moduleService.moduleSave(module);
				msg =new JSONMessageView(0,"添加成功!",null);	
			}			
			return msg;			
		} catch (Exception e) {
			e.printStackTrace();
			msg =new JSONMessageView(-1,"操作失败!",null);
			return msg;
		}	
	}
	
	
	/**
	 * 删除模块
	 * @param id
	 * @return
	 */
	@RequestMapping("/module_del.htm")
	public JSONMessageView module_del(@RequestParam(value="id")String id){
		JSONMessageView msg = new JSONMessageView();
		try {
			moduleService.moduleDel(StringUtil.formatParamLike(id));
			msg =new JSONMessageView(0,"删除成功!",null);
			return msg;			
		} catch (Exception e) {
			e.printStackTrace();
			msg =new JSONMessageView(-1,"删除失败!",null);
			return msg;
		}	
	}
	/**
	 * 跳转到绑定操作页面
	 * @return
	 */
	@RequestMapping("/roleAddModule.htm")
	public ModelAndView roleAddModule(@RequestParam(value="roleId")String roleId,Model model){
		ModelAndView mv = new ModelAndView("back/bind-admin-permission");
		model.addAttribute("roleId", roleId);
		return mv;
	}
	
}
